package com.vue;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.util.*;

import com.metier.*;
import com.modele.ModeleBorne;
import com.persistance.AccesData;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Classe Jpanel d'affichage des bornes par station qui sera integre au Jform
 * principale
 * 
 * @author b0dian
 * @version 1.0
 *
 */
public class BorneParStation extends JPanel {
	private JLabel lblStation;
	private JComboBox<String> comboBox;
	private JScrollPane scrollPane;
	private JTable table;
	private ModeleBorne modele;
	private List<Station> LesStations;

	/**
	 * Create the panel.
	 */
	public BorneParStation() {
		setLayout(null);
		add(getLblStation());
		add(getScrollPane());
		add(getComboBox());

	}

	/**
	 * Create label
	 */
	private JLabel getLblStation() {
		if (lblStation == null) {
			lblStation = new JLabel("Station");
			lblStation.setBounds(93, 64, 46, 14);
		}
		return lblStation;
	}

	/**
	 * Create ComboBox Et rempli cette comboxBox avec les libelle de stations
	 * recuperes grace au accesData Il y a egalement un evènement qui modifiera
	 * le contenu de la Jtable en fonction de l'option choisie dans la comboBox
	 */
	private JComboBox<String> getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox<String>();
			comboBox.setBounds(248, 61, 144, 20);
			LesStations = new ArrayList<Station>();
			LesStations = AccesData.getStations();
			for (Station s : LesStations) {
				comboBox.addItem(s.getLibelleEmplacement());
			}
			comboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (comboBox.getSelectedIndex() != -1) {
						modele = new ModeleBorne(LesStations.get(
								comboBox.getSelectedIndex()).getIdStation());
						table.setModel(modele);
						// Raffraichir le Modele
						table.revalidate();
					}
				}
			});
			comboBox.setSelectedIndex(0);
		}
		return comboBox;
	}

	/**
	 * Create Jtable En lui passant en parramètre le modele correspondant
	 */
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table = new JTable(modele);
			table.setBounds(31, 95, 395, 144);
		}
		return table;
	}

	/**
	 * Create scrollPane Auquel nous integrerons le Jtable
	 */
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(20, 111, 395, 100);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
}
